/**
 * 
 */
package com.gwcloud.leetcode.hot100;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.gwcloud.leetcode.common.datastructure.TreeNode;

/**
 * @ClassName   : Leetcode0102 
 * @author      : ganwei 
 * @date        : 2020年5月20日
 * @Description : Leetcode0102
 */
public class Leetcode0102 {
	List<List<Integer>> result = new ArrayList<>();
    /**
     * -二叉树，层序遍历，广度优先遍历
     */
	public List<List<Integer>> levelOrder(TreeNode root) {
    	
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		queue.offer(root);
		
		while(!queue.isEmpty()){
			int length = queue.size();
			
			List<Integer> l = new ArrayList<Integer>();
			for(int i = 0; i < length; i++){
				TreeNode node = queue.poll();
				
				l.add(node.val);
				
				if(node.left != null){
					queue.offer(node.left);
				}
				if(node.right != null){
					queue.offer(node.right);
				}
			}
			result.add(l);
		}
		
		
    	return result;
    }
	
	public static void main(String[] args){
		TreeNode t1 = new TreeNode(3);
		TreeNode t2 = new TreeNode(9);
		TreeNode t3 = new TreeNode(20);
		TreeNode t4 = new TreeNode(15);
		TreeNode t5 = new TreeNode(7);
		
		t1.left = t2;
		t1.right = t3;
		
		t3.left = t4;
		t3.right = t5;
		
		Leetcode0102 l0102 = new Leetcode0102();
		l0102.levelOrder(t1);
		System.out.println(l0102.result);
		
	}
}
